#!/bin/bash

# ----------------------------------------------------------------------
# Filename:   genhomedircon.sh
# Version:    1.0
# Date:       2013/12/17
# Author:     huijing.hei
# Email:      huijing.hei@cs2c.com.cn
# Summary:    03系统安全功能-01SELinux-03SELinux重要命令-03SELinux安全上下文标签相关命令-14genhomedircon
# Notes:       ***
# Copyright:    China Standard Software Co., Ltd.
# History：     
#             Version 1.0, 2013/12/17
#             - genhomedircon
# ----------------------------------------------------------------------

##! @TODO: enforcing
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure

function DoSetup()
{
    trap DoCleanup EXIT INT 

    IsRoot
    EchoResult "root"

    CmdCheck getenforce genhomedircon

    # check selinux status is enforcing or permissive
    CheckSElinuxStatus
    EchoResult "check selinux status is enforcing or permissive"

    # set selinux enforcing
    SELINUX_STATUS=`getenforce | tr A-Z a-z`
    SetSElinux enforcing
    EchoResult "set selinux enforcing"

    # check homedirs    
    HOMEDIR=/etc/selinux/targeted/contexts/files/file_contexts.homedirs
    [ -f ${HOMEDIR} ] && rm -f ${HOMEDIR}

}

##! @TODO: 
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure
function DoTest()
{
    cmd="genhomedircon"
    $cmd
    EchoResult "$cmd"

    [ -s ${HOMEDIR} ]
    EchoResult "${HOMEDIR} is generated"

}
##! @TODO: cleanup
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure

function DoCleanup()
{
    # restore selinux
    SetSElinux ${SELINUX_STATUS}
    EchoResult "restore selinux to ${SELINUX_STATUS}"
}

# include lib files

if [ -z "$SFROOT" ]
then
    echo "SFROOT is null, pls check"
    exit 1
fi

. ${SFROOT}/lib/Echo.sh
. ${SFROOT}/lib/Check.sh
. ${SFROOT}/testcases/SELinux/lib/Selib.sh
. ${SFROOT}/testcases/SELinux/lib/UserOps.sh

export LANG=C

DoSetup
DoTest
EchoResult "testcase: selinux- genhomedircon"
echo ""
